package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.mapper.BSendRecordMailGatherMapper;
import com.youhome.service.model.BSendRecordMailGather;
import com.youhome.service.model.BSendRecordMailGatherWithBLOBs;
import com.youhome.service.model.BUser;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BSendRecordMailGatherService;
import com.youhome.service.service.BUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by chen365 on 2017/12/29.
 */
@Service
public class BSendRecordMailGatherServiceImpl implements BSendRecordMailGatherService {
    @Autowired
    private BSendRecordMailGatherMapper bSendRecordMailGatherMapper;

    @Override
    public int create(BSendRecordMailGatherWithBLOBs record) {
        return bSendRecordMailGatherMapper.insertSelective(record);
    }

    @Override
    public BSendRecordMailGather selectByPrimaryKey(Long id) {
        return bSendRecordMailGatherMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<BSendRecordMailGather> selectByBean(Integer pageIndex, Integer pageSize, BSendRecordMailGather record) {
        if (StringUtils.isEmpty(pageIndex) || pageIndex < 1) {
            pageIndex = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<BSendRecordMailGather> list = bSendRecordMailGatherMapper.selectByBean(record);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<BSendRecordMailGather> selectByBean(Principal principal, Integer pageNumber, Integer pageSize) throws CustomException {
        Long userId = principal.getUser().getUserId();

        BSendRecordMailGather bSendRecordMail = new BSendRecordMailGather();
        bSendRecordMail.setUserId(userId);

        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<BSendRecordMailGather> list = bSendRecordMailGatherMapper.selectByBean(bSendRecordMail);
        return new PageInfo<>(list);

    }

}
